import 'package:flt_bmflocation/flt_bmflocation.dart';

class BaiduPoiList {
  //poi标签
  String? tags;
  //uid（仅iOS返回）
  String? uid;
  //poi名称
  String? name;
  //poi地址
  String? addr;

  BaiduPoiList({this.tags, this.uid, this.name, this.addr});

  BaiduPoiList.fromMap(Map map)
      : assert(
            map != null, // ignore: unnecessary_null_comparison
            'Construct a BaiduPoiList，The parameter map cannot be null !') {
    tags = map['tags'];
    uid = map['uid'];
    name = map['name'];
    addr = map['addr'];
  }

  Map<String, Object?> toMap() {
    return {
      'tags': tags,
      'uid': uid,
      'name': name,
      'addr': addr,
    };
  }
}

/// 百度定位结果类，用于存储各类定位结果信息
class FlsBaiduLocation {
  ///定位id
  String? locationID;

  /// 定位成功时间
  String? locTime;

  /// 定位结果类型(仅Android端返回)
  int? locType;

  /// 航向
  double? course;

  /// 水平精度（仅iOS端返回）
  double? horizontalAccuracy;

  /// 垂直精度（仅iOS端返回）
  double? verticalAccuracy;

  /// 定位精度（仅Android端返回）
  double? radius;

  /// 速度
  double? speed;

  /// 纬度
  double? latitude;

  /// 经度
  double? longitude;

  /// 高度
  double? altitude;

  /// 国家
  String? country;

  /// 省份
  String? province;

  /// 城市
  String? city;

  /// 区县
  String? district;

  //乡镇
  String? street;

  /// 街道
  String? town;

  /// 地址
  String? address;

  /// 行政区划编码
  String? adCode;

  /// 城市编码
  String? cityCode;

  /// 街道号码
  String? streetNumber;

  /// 位置语义化描述，例如"在百度大厦附近"
  String? locationDetail;

  /// 模拟定位概率
  FlsMockLocationProbability? probability;

  /// 周边poi信息
  List<BaiduPoiList>? pois;

  /// 周边poi信息，每个poi之间用"|"隔开
  @Deprecated('已废弃since3.0.0，推荐使用 `pois`')
  String? poiList;

  Map? poiRegion;

  /// 定位结果回调时间
  String? callbackTime;

  /// 错误码
  int? errorCode;

  /// 定位失败描述信息
  String? errorInfo;

  FlsBaiduLocation(
      {this.town,
      this.course,
      this.speed,
      this.locationID,
      this.locTime,
      this.locType,
      this.radius,
      this.latitude,
      this.longitude,
      this.altitude,
      this.country,
      this.adCode,
      this.cityCode,
      this.streetNumber,
      this.province,
      this.city,
      this.horizontalAccuracy,
      this.verticalAccuracy,
      this.district,
      this.street,
      this.address,
      this.locationDetail,
      this.probability,
      this.poiList,
      this.pois,
      this.poiRegion,
      this.callbackTime,
      this.errorCode,
      this.errorInfo});

 // BaiduLocation  转 FlsBaiduLocation 
  FlsBaiduLocation.fromBaiduLocation(BaiduLocation baiduLocation) {
    town = baiduLocation.town;
    course = baiduLocation.course;
    locationID = baiduLocation.locationID;
    locTime = baiduLocation.locTime;
    locType = baiduLocation.locType;
    speed = baiduLocation.speed;
    radius = baiduLocation.radius;
    horizontalAccuracy = baiduLocation.horizontalAccuracy;
    verticalAccuracy = baiduLocation.verticalAccuracy;
    latitude = baiduLocation.latitude;
    longitude = baiduLocation.longitude;
    altitude = baiduLocation.altitude;
    country = baiduLocation.country;
    province = baiduLocation.province;
    city = baiduLocation.city;
    adCode = baiduLocation.adCode;
    cityCode = baiduLocation.cityCode;
    streetNumber = baiduLocation.streetNumber;
    district = baiduLocation.district;
    street = baiduLocation.street;
    address = baiduLocation.address;
    locationDetail = baiduLocation.locationDetail;
    poiList = baiduLocation.poiList;
    if (baiduLocation.pois != null) {
      List<BaiduPoiList> tmpPoiInfoList = [];
      baiduLocation.pois!.forEach((v) {
        tmpPoiInfoList.add(BaiduPoiList.fromMap(v.toMap()));
      });

      pois = List.from(tmpPoiInfoList);
    }
    if (baiduLocation.probability != null) {
      FlsMockLocationProbability pbb = FlsMockLocationProbability.values[baiduLocation.probability!.index];
      probability = pbb;
    }
    if (baiduLocation.poiRegion != null) {
      poiRegion = Map.from(baiduLocation.poiRegion!);
    }
    callbackTime = baiduLocation.callbackTime;
    errorCode = baiduLocation.errorCode;
    errorInfo = baiduLocation.errorInfo;
  }

  // FlsBaiduLocation 转 BaiduLocation
  BaiduLocation toBaiduLocation() {
    BaiduLocation baiduLocation = BaiduLocation();
    baiduLocation.town = town;
    baiduLocation.course = course;
    baiduLocation.locationID = locationID;
    baiduLocation.locTime = locTime;
    baiduLocation.locType = locType;
    baiduLocation.speed = speed;
    baiduLocation.radius = radius;
    baiduLocation.horizontalAccuracy = horizontalAccuracy;
    baiduLocation.verticalAccuracy = verticalAccuracy;
    baiduLocation.latitude = latitude;
    baiduLocation.longitude = longitude;
    baiduLocation.altitude = altitude;
    baiduLocation.country = country;
    baiduLocation.province = province;
    baiduLocation.city = city;
    baiduLocation.adCode = adCode;
    baiduLocation.cityCode = cityCode;
    baiduLocation.streetNumber = streetNumber;
    baiduLocation.district = district;
    baiduLocation.street = street;
    baiduLocation.address = address;
    baiduLocation.locationDetail = locationDetail;
    baiduLocation.poiList = poiList;
    if (pois != null) {
      List<Map> tmpPoiInfoList = [];
      pois!.forEach((v) {
        tmpPoiInfoList.add(v.toMap());
      });

      baiduLocation.pois = List.from(tmpPoiInfoList);
    }
    if (probability != null) {
      baiduLocation.probability = probability as BMFMockLocationProbability?;
    }
    if (poiRegion != null) {
      baiduLocation.poiRegion = Map.from(poiRegion!);
    }
    baiduLocation.callbackTime = callbackTime;
    baiduLocation.errorCode = errorCode;
    baiduLocation.errorInfo = errorInfo;
    return baiduLocation;
  }

  /// 转map 
  Map<String, Object?> toMap() {
    List<Map> tmpPoiInfoList = [];
    if (pois != null) {
      pois!.forEach((v) {
        tmpPoiInfoList.add(v.toMap());
      });
    }

    return {
      'town': town,
      'course': course,
      'locationID': locationID,
      'locTime': locTime,
      'locType': locType,
      'speed': speed,
      'radius': radius,
      'horizontalAccuracy': horizontalAccuracy,
      'verticalAccuracy': verticalAccuracy,
      'latitude': latitude,
      'longitude': longitude,
      'altitude': altitude,
      'country': country,
      'province': province,
      'city': city,
      'adCode': adCode,
      'cityCode': cityCode,
      'streetNumber': streetNumber,
      'district': district,
      'street': street,
      'address': address,
      'locationDetail': locationDetail,
      'probability': probability?.index,
      'pois': tmpPoiInfoList,
      'poiRegion': poiRegion,
      'callbackTime': callbackTime,
      'errorCode': errorCode,
      'errorInfo': errorInfo,
    };
  }
}

// 模拟定位概率
enum FlsMockLocationProbability { none, low, mid, high }
